package controllers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import models.Noticia;
import play.db.DB;
import play.mvc.Controller;

public class NoticiaController extends Controller {
	
	public static void indexPublico() {
		if(!Application.isLogged()) return;
		
		String menu = Application.menubasic;
    	String rodape = Application.rodapebasic;
    	if(Application.sessao != null && Application.sessao.get("tipo") != null) {
    		if(Application.sessao.get("tipo").compareTo("0") == 0){
    			menu = Application.menubasic;
    		}
    		else{
    			menu = Application.menufull;
    		}
    	}
		
		//Mostra a tela do cadastro de noticia
		if(Integer.parseInt(Application.sessao.get("tipo")) != 0)
			render("Application/cadNoticia.html", menu, rodape);
		else {
			String msg = "Acesso não autorizado";
			String script = "<script language=\"javascript\" type=\"text/javascript\">";
    		script += "alert ( \" " + msg + "\" ) </script> ";
    		boolean logado = true;
			render("Application/index.html", script, logado, menu, rodape);
		}
	}
	
	public static void indexPrivado() {
		if(!Application.isLogged()) return;
		
		String menu = Application.menubasic;
    	String rodape = Application.rodapebasic;
    	if(Application.sessao != null && Application.sessao.get("tipo") != null) {
    		if(Application.sessao.get("tipo").compareTo("0") == 0){
    			menu = Application.menubasic;
    		}
    		else{
    			menu = Application.menufull;
    		}
    	}
		
		//Mostra a tela do cadastro de mensagem
		render("Application/cadMensagem.html", menu, rodape);
	}
	
	public static void exibicao() {
		boolean logado = Application.isLogged2(); 
		
		String menu = Application.menubasic;
    	String rodape = Application.rodapebasic;
    	if(Application.sessao != null && Application.sessao.get("tipo") != null) {
    		if(Application.sessao.get("tipo").compareTo("0") == 0){
    			menu = Application.menubasic;
    		}
    		else{
    			menu = Application.menufull;
    		}
    	}
		
		try {
			List<Noticia> lis = Noticia.all(!logado);
			// Mostra a tela de exibicao
			render("Application/exibicaoNoticia.html", lis, logado, menu, rodape);
		}
		catch(SQLException ex) {
			String msg = "O codigo SQL teve um comportamento inesperado!";
    		
    		// Java Script mostrando a mensagem de erro!
            String script = "<script language=\"javascript\" type=\"text/javascript\">";
            		script += "alert ( \" " + msg + "\" ) </script> ";
           	render("Application/cadNoticia.html", script, logado, menu, rodape);
            return;
		}
    }
	
	public static void exibicaoNoticia(int codigoNoticia) {
		boolean logado = Application.isLogged2();
		
		String menu = Application.menubasic;
    	String rodape = Application.rodapebasic;
    	if(Application.sessao != null && Application.sessao.get("tipo") != null) {
    		if(Application.sessao.get("tipo").compareTo("0") == 0){
    			menu = Application.menubasic;
    		}
    		else{
    			menu = Application.menufull;
    		}
    	}
		
		try {
			Noticia not = Noticia.specific(codigoNoticia);
			
			render("Application/exibirNoticia.html", not, menu, rodape, logado);
		}
		catch(SQLException ex) {
			String msg = "O codigo SQL teve um comportamento inesperado!";
    		
    		// Java Script mostrando a mensagem de erro!
            String 	script = "<script language=\"javascript\" type=\"text/javascript\">";
            		script += "alert ( \" " + msg + "\" ) </script> ";
            render("Application/exibicaoNoticia.html", script, menu, rodape, logado);
            return;
		}
		
	}
	
	public static void add(boolean publica){
		if(!Application.isLogged()) return;
		
		String menu = Application.menubasic;
    	String rodape = Application.rodapebasic;
    	if(Application.sessao != null && Application.sessao.get("tipo") != null) {
    		if(Application.sessao.get("tipo").compareTo("0") == 0){
    			menu = Application.menubasic;
    		}
    		else{
    			menu = Application.menufull;
    		}
    	}
		
		Calendar currdate = Calendar.getInstance();
		Noticia not = new Noticia();
		String data = ""+currdate.get(Calendar.DATE)+'/'+(currdate.get(Calendar.MONTH)+1)+'/'+currdate.get(Calendar.YEAR);
		data += " " + (currdate.get(Calendar.HOUR_OF_DAY)) + ":" + currdate.get(Calendar.MINUTE);
		String msg = "";
		
		try {
			Timestamp date = new Timestamp( new SimpleDateFormat("dd/MM/yyyy hh:mm").parse(data).getTime() );
			not.setDataDeCadastro(date);
        }
        catch(ParseException ex) {
        	msg += "O horario nao esta em formato valido. O formato correto e hh:mm.";
        	
        	// Java Script mostrando a mensagem de erro!
            String 	script = "<script language=\"javascript\" type=\"text/javascript\">";
            		script += "alert ( \" " + msg + "\" ) </script> ";
            render("Application/cadNoticia.html", script, menu, rodape);
            
        	return;
        }
		
		not.setTitulo(params.get("titulo"));
		not.setConteudo(params.get("conteudoNoticia"));
		not.setPublica(publica);
		not.setCodigoUsuario(Integer.parseInt(Application.sessao.get("codigo")));
		
		// Validacao dos campos
    	if (not.getTitulo().isEmpty()) {
    		msg += "Titulo inválido!";
    	} else if (not.getConteudo().isEmpty()) {
        	msg += "Mensagem inválida!";
        } else {
        	try {
        		not.save();
        		exibicao();
        	}
        	catch(SQLException ex) {
        		msg = "O codigo SQL teve um comportamento inesperado!";
    		
        		// Java Script mostrando a mensagem de erro!
        		String 	script = "<script language=\"javascript\" type=\"text/javascript\">";
            	script += "alert ( \" " + msg + "\" ) </script> ";
            	render("Application/cadNoticia.html", script, menu, rodape);
            	return;
        	}
        }
	}
	
	public static void excluirNoticia(int codigoNoticia) {
		if(!Application.isLogged()) return;
		
		Noticia.delete(codigoNoticia);
		
		exibicao();
	}
}
